<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑课程</title>
    <script src="../js/jquery-1.7.2.js"></script>
</head>
<body>
<h1>编辑课程</h1>
<p>课程号: <span id="id"></span></p>
<p>课程名称: <input type="text" name="courseName" placeholder="课程名称"></p>
<p>授课教师: <input type="text" name="teacherName" placeholder="授课教师"></p>
<p>教室容量: <span id="croomCapacity"></span></p>
<p>
    <button id="edit_btn">确认保存</button>
</p>
<script>
    $(function (){
        //获取url中的参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
        }
        let cid = getUrlParam("cid");
        $.ajax({
            type:"get",
            url:"/course/findOne/"+cid,
            success: function(r){
                if(r.ok){
                    let course = r.result.data;
                    $("#id").html(course.id);
                    $("input[name='courseName']").val(course.courseName);
                    $("input[name='teacherName']").val(course.teacherName);
                    $("#croomCapacity").html(course.croomCapacity);
                }else{
                    alert(r.msg)
                }
            }
        });
        $("#edit_btn").click(function () {
            let course = {};
            course.id = $("#id").html();
            course.courseName = $("input[name='courseName']").val();
            course.teacherName = $("input[name='teacherName']").val();
            $.ajax({
                type: "POST",
                url: "/course/update",
                data: JSON.stringify(course),
                contentType: "application/json",
                success: function (r) {
                    if (r.ok) {
                        window.location = "/courseMgr.html";
                    } else {
                        alert(r.msg)
                    }
                }
            });
        })
    })
</script>
</body>
</html>